﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LogLib;
using PhotoNamingService;

namespace FormPageConfigs.ChildPages
{
    public partial class PageProductClient : PageConfigBase
    {
        private ProductConfig _config = null;
        public PageProductClient()
        {
            InitializeComponent();
            _config = ProductConfig.Instance;
        }

        public override bool InitCtrls()
        {
            metroComboBoxDeviceType.SelectedIndex = 0;
            for (int i = 0; i < metroComboBoxDeviceType.Items.Count; i++)
            {
                if (_config.DeviceType.Equals(metroComboBoxDeviceType.Items[i].ToString()))
                {
                    metroComboBoxDeviceType.SelectedIndex = i;
                }                   
            }
            textBoxSerialNumber.Text = _config.DeviceID;
            textBoxCityCode.Text = _config.CityCode;
            comboBoxLocalID.SelectedIndex = 0;
            comboBoxTakePhotoType.SelectedIndex = int.Parse(_config.TakePhotoType);
            for (int i = 0; i < comboBoxLocalID.Items.Count; i++)
            {
                if (_config.LocalId.Equals(comboBoxLocalID.Items[i].ToString()))
                {
                    comboBoxLocalID.SelectedIndex = i;
                }
            }
            return true;
        }

        public override bool UpdateData()
        {
            if (metroComboBoxDeviceType.SelectedIndex == -1)
            {
                MessageBox.Show("设备型号不能为空.");
                return false;
            }
            if (comboBoxLocalID.SelectedIndex == -1)
            {
                MessageBox.Show("设备本地编号不能为空");
                return false;
            }
            string deviceType = metroComboBoxDeviceType.SelectedItem.ToString();
            string deviceID =textBoxSerialNumber.Text;
            string localID = comboBoxLocalID.SelectedItem.ToString();
            string cityCode = textBoxCityCode.Text;
            string takePhotoType = comboBoxTakePhotoType.SelectedIndex.ToString();
            ProductConfig.SetConfig(deviceType, deviceID, cityCode, localID, takePhotoType);
            return true;
        }

        public override bool LoadConfig()
        {
            if (!File.Exists(_config.ConfigFilePath))
            {
                _config.Save();
            }

            bool bRet = _config.Load();
            if (!_config.Load())
            {
                _error = _config.ErrorMessage;
                Log.WriteAsync(_error);
            }
            return bRet;
        }

        private void PageProductClient_Load(object sender, EventArgs e)
        {

        }
    }
}
